package com.gitee.dupe.msg.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.gitee.dolt.mail.event.MailSendLogListener;
import com.gitee.dolt.sms.event.SmsSendLogListener;
import com.gitee.dolt.utils.NumberUtils;
import com.gitee.dupe.msg.entity.MsgLog;
import com.gitee.dupe.msg.enumeration.MsgTypeEnum;
import com.gitee.dupe.msg.service.IMsgLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LYH
 */
@Configuration
@RequiredArgsConstructor
public class MsgLogConfiguration {

    private final IMsgLogService msgLogService;

    @Bean
    public MailSendLogListener mailSendLogListener() {
        return new MailSendLogListener(mailLog -> {
            MsgLog msgLog = new MsgLog().setType(MsgTypeEnum.EMAIL).setHtml(mailLog.getHtml())
                    .setRecipients(CollUtil.join(mailLog.getRecipients(), StrUtil.COMMA))
                    .setFroms(mailLog.getFrom())
                    .setSubject(mailLog.getSubject()).setContent(mailLog.getContent())
                    .setSuccess(mailLog.getSuccess()).setResult(mailLog.getResult())
                    .setJson(JSONUtil.toJsonStr(mailLog))
                    .setSentTime(mailLog.getSentDate());
            msgLogService.save(handlerMsgLog(msgLog));
        });
    }

    @Bean
    @ConditionalOnClass(SmsSendLogListener.class)
    public SmsSendLogListener smsSendLogListener() {
        return new SmsSendLogListener(smsLog -> {
            MsgLog msgLog = new MsgLog().setType(MsgTypeEnum.SMS).setHtml(false)
                    .setRecipients(smsLog.getPhoneNumbers())
                    .setSubject(smsLog.getTemplateId()).setContent(smsLog.getParams())
                    .setSuccess(smsLog.getSuccess()).setResult(smsLog.getMessage())
                    .setJson(JSONUtil.toJsonStr(smsLog))
                    .setSentTime(smsLog.getSentDate());
            msgLogService.save(handlerMsgLog(msgLog));
        });
    }


    private MsgLog handlerMsgLog(MsgLog msgLog) {
        String recipients = msgLog.getRecipients();
        String subject = msgLog.getSubject();
        String content = msgLog.getContent();
        String result = msgLog.getResult();
        String json = msgLog.getJson();
        if (StrUtil.length(recipients) > NumberUtils.NUM_255) {
            msgLog.setRecipients(StrUtil.subSufByLength(recipients, NumberUtils.NUM_255));
        }
        if (StrUtil.length(subject) > NumberUtils.NUM_255) {
            msgLog.setSubject(StrUtil.subSufByLength(subject, NumberUtils.NUM_255));
        }
        if (StrUtil.length(result) > NumberUtils.NUM_255) {
            msgLog.setResult(StrUtil.subSufByLength(result, NumberUtils.NUM_255));
        }
        if (StrUtil.length(content) > NumberUtils.NUM_65535) {
            msgLog.setContent(StrUtil.subSufByLength(content, NumberUtils.NUM_65535));
        }
        if (StrUtil.length(json) > NumberUtils.NUM_65535) {
            msgLog.setJson(StrUtil.subSufByLength(json, NumberUtils.NUM_65535));
        }
        return msgLog;
    }
}
